package com.project2.service;

import com.project2.bean.po.*;
import com.project2.bean.vo.WxSearchIndexVo;
import com.project2.mapper.MarketKeywordMapper;
import com.project2.mapper.MarketSearchHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: MH
 * @Date: 2022-08-14 21:07
 * @Description: 前台搜索业务的具体实现
 */
@Service
public class WxSearchServiceImpl implements WxSearchService {
    @Autowired
    MarketKeywordMapper marketKeywordMapper;
    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;

    /**
    *@Author: MH
    *@date: 2022/8/14 22:42
    *@Description:用户输入搜索信息后返回信息业务具体实现
    */
    @Override
    public WxSearchIndexVo showSearchKeywordInfo(HttpServletRequest request) {
        String header = request.getHeader("X-CskaoyanMarket-Token");
        MarketUser user = (MarketUser) request.getSession().getAttribute(header);
        if (user == null) {
            return null;
        }
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();
        criteria.andIsDefaultEqualTo(true);
        criteria.andDeletedEqualTo(false);
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(marketKeywordExample);
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria criteria2 = marketSearchHistoryExample.createCriteria();
        criteria2.andDeletedEqualTo(false);
        List<MarketSearchHistory> marketSearchHistories = marketSearchHistoryMapper.selectByExample(marketSearchHistoryExample);
        MarketKeywordExample marketKeywordExample1 = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria1 = marketKeywordExample1.createCriteria();
        criteria1.andIsHotEqualTo(true);
        criteria1.andDeletedEqualTo(false);
        List<MarketKeyword> marketKeywords1 = marketKeywordMapper.selectByExample(marketKeywordExample1);
        WxSearchIndexVo wxSearchIndexVo = new WxSearchIndexVo();
        wxSearchIndexVo.setDefaultKeyword(marketKeywords);
        wxSearchIndexVo.setHistoryKeywordList(marketSearchHistories);
        wxSearchIndexVo.setHotKeywordList(marketKeywords1);
        return wxSearchIndexVo;
    }

    /**
    *@Author: MH
    *@date: 2022/8/14 22:43
    *@Description:对所有搜索历史的逻辑删除的具体实现,直接将所有的deleted改为true即可
    */
    @Override
    public void deleteSearchHistory() {
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        MarketSearchHistory marketSearchHistory = new MarketSearchHistory();
        marketSearchHistory.setDeleted(true);
        marketSearchHistoryMapper.updateByExampleSelective(marketSearchHistory, marketSearchHistoryExample);
    }

    /**
    *@Author: MH
    *@date: 2022/8/14 22:44
    *@Description:在用户搜索时,对用户输入的关键字进行提示业务的实现,
     *            直接对用户输入的关键字进行模糊查询即可
     *
    */
    @Override
    public List<String> searchKeywordHelper(String keyword, HttpServletRequest request) {
        String header = request.getHeader("X-CskaoyanMarket-Token");
        MarketUser user = (MarketUser) request.getSession().getAttribute(header);
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();
        criteria.andKeywordLike("%" + keyword + "%");
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(marketKeywordExample);
        if(marketKeywords==null){
            return null;
        }
        ArrayList<String> strings = new ArrayList<>();
        for (MarketKeyword marketKeyword : marketKeywords) {
            strings.add(marketKeyword.getKeyword());
        }
        return strings;
    }

}
